﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class IMGradeEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["Id"] != null)
                {
                    int id = int.Parse(Request.Params["Id"]);
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    CMS_IMGrade model = ctx.CMS_IMGrade.Single(q => q.Id == id);
                    Grade.Value = model.Grade;
                    LowerPenetration.Value = model.LowerPenetration.ToString();
                    HigherPenetration.Value = model.HigherPenetration.ToString();
                    Remarks.Value = model.Remarks;
                    OrderNo.Value = model.OrderNo.ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            CMS_IMGrade model;
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                model = ctx.CMS_IMGrade.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                model = new CMS_IMGrade();
                ctx.CMS_IMGrade.InsertOnSubmit(model);
            }
            model.Grade = Grade.Value;
            model.LowerPenetration = int.Parse(LowerPenetration.Value);
            if (!string.IsNullOrWhiteSpace(HigherPenetration.Value))
            {
                model.HigherPenetration = int.Parse(HigherPenetration.Value);
            }
            model.Remarks = Remarks.Value;
            model.OrderNo = int.Parse(OrderNo.Value);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}